if (!window.ossmoney) {
	window.ossmoney = {};
}

function validateAndsubmit(id) {
    var form = dijit.byId(id);
    if (form.validate()) {
        form.submit();
    }
}

function negatives(index, data) {
	var value = data !== null ? data.balance[0] : 0;
	return 0 > value ? '(' + value + ')' : value === 0 ? "" : String(value);
}

function doubleFormatter(value, idx) {
	var data = isNaN(value) ? Number(value.substring(1, value.length - 1)) : Number(value);
	var format = value == 0 ? "" : dojo.number.format(data);
	var retData = isNaN(value) ? '<span style="color:red">(' + format + ')</span>' : format;
	return retData != '<span style="color:red">(-0)</span>' ? retData : "0";
}

function limitedStringFormatter(value, idx) {
	return '<span title="' + value + '" style="display:inline-block;overflow:hidden;text-overflow:ellipsis;height:13px">' + value + '</span>';
}

function booleanFormatter(value, idx) {
	return value ? "X" : "";
}

function styleRows(inRow) {
	var time = new Date().getTime();
	var dateString = this.store._arrayOfAllItems[inRow.index].operationDate[0];
	var parts = dateString.split("-");
	var date = new Date(Number(parts[0]), Number(parts[1]) - 1, Number(parts[2]));
	if (date.getTime() > time) {
		inRow.customClasses += ' future';
	} else {
		if(inRow.odd) {
			inRow.customClasses += ' dojoxGridRowOdd';
		}
		if(inRow.over) {
			inRow.customClasses += ' dojoxGridRowOver';
		}
		if(inRow.selected) {
			inRow.customClasses += ' dojoxGridRowSelected';
		}
	}
}

function sanitizePieChartData(origin, category, amount) {
	var total = 0;
	for (var index = 0; index < origin.length; index++) {
		total += origin[index].y;
	}
	var sanitized = [];
	var other = {x: "1", y: 0, tooltip: "<table><thead><th>" + category + "</th><th>" + amount + "</th></thead><tbody>"};
	for (var index2 = 0; index2 < origin.length; index2++) {
		if ((origin[index2].y / total) > 0.03) {
			sanitized.push(origin[index2]);
		} else {
			other.y += origin[index2].y;
			var tooltip = origin[index2].tooltip.split(":");
			other.tooltip += "<tr><td>" + tooltip[0] + "</td><td>" + tooltip[1] + "</td></tr>";
		}
	}
	if (other.y > 0) {
		other.tooltip += "</tbody></table>";
		sanitized.push(other);
	}
	return sanitized;
}

function getBillingDates(dateObject, locale) {
	return hasPendingBills(dateObject) ? "redfuture" : "";
}

function isDisabledDate(dateObject, locale) {
	var today = new Date();
	today.setDate(today.getDate() - 1);
	if (dateObject.getTime() < today.getTime()) {
		if (hasPendingBills(dateObject)) {
			return false;
		}
		return true;
	}
	return false;
}

function hasPendingBills(dateObject) {
	var hasBill = false;
	if (window.ossmoney.billingDates) {
		for (var index = 0; index < window.ossmoney.billingDates.length; index++) {
			hasBill |= ((window.ossmoney.billingDates[index].getDate() == dateObject.getDate()) && (window.ossmoney.billingDates[index].getMonth() == dateObject.getMonth()));  
		}
	}
	return hasBill;
}

function negate() {
	var amount = dijit.byId('transaction.amount').getValue();
	if (isNaN(amount)) {
		amount = 0;
	}
	amount = Math.abs(amount);
	var category = dijit.byId('transaction.subcategory').getDisplayedValue();
	if ((category.indexOf("(E)") >= 0) || (category.indexOf("(G)") >= 0)) {
		amount *= -1;
	}
	dijit.byId('transaction.amount').setValue(amount);
}

function summaryFormatter(value, rowIdx) {
	return Number(value) === 0 ? "" : String(value).replace(/^0*/gi,"").replace(/ /gi, ""); 
}

function ellipsisall(value, rowIdx) {
	return eurospace(99, value);
}

function ellipsis(value, rowIdx) {
	var ptg = dojo.isFF ? "80" : "85";
	return eurospace(ptg, value);
}

function eurospace(ptg, value) {
	var txt = '<span style="display:inline-block;white-space:nowrap;width:' + ptg + '%;overflow:hidden;text-overflow:ellipsis;height:15px">' + value + "</span>";
	return txt.replace(/ €/gi, "€");
}